package ClientCommTests;

import static org.junit.Assert.*;

import org.junit.Test;

import client.TelnetClient;
import clientCommunicationHandler.ClientServerMsgHandler;
import clientCommunicationHandler.UserInterfaceMsgHandler;

public class clientServerCommunicationTest {
	private final String serverDelimiter = (char)1+"";
	@Test
	public void testviewForumWithServerReplay(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String [] initParseInput = "1. view_Forums\n".split(" ");
		parser.initParse(initParseInput);
		String demoServerMsg = "threadsList"+serverDelimiter+"admin"+serverDelimiter+"1:temp1"+serverDelimiter;
		TelnetClient.userName = "admin";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String goingToSendToServer = parser.parserUserMsg(3, TelnetClient.menu.toString().split("\n"));
		String expectedAns = "moderatorsCandidate"+serverDelimiter+"admin"+serverDelimiter+"1"+serverDelimiter;
		assertEquals(expectedAns,goingToSendToServer);
		
	}
	@Test
	public void testRemoveModeratorWithServerReplay(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String [] initParseInput = "1. view_Forums\n".split(" ");
		parser.initParse(initParseInput);
		String demoServerMsg = "threadsList"+serverDelimiter+"admin"+serverDelimiter+"1:temp1"+serverDelimiter;
		TelnetClient.userName = "admin";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String goingToSendToServer = parser.parserUserMsg(4, TelnetClient.menu.toString().split("\n"));
		String expectedAns = "getModerators"+serverDelimiter+"admin"+serverDelimiter+"1"+serverDelimiter;
		assertEquals(expectedAns,goingToSendToServer);
		demoServerMsg = "ModeratorsList"+serverDelimiter+"admin"+serverDelimiter+"u1"+serverDelimiter+"u2"+serverDelimiter+"";
		tMsgHandler.parseServerMsg(demoServerMsg);
		goingToSendToServer = parser.parserUserMsg(2, TelnetClient.menu.toString().split("\n"));
		expectedAns = "removeModerator"+serverDelimiter+"admin"+serverDelimiter+"1"+serverDelimiter+"u1"+serverDelimiter+"\n\0\nviewForum"+serverDelimiter;
		assertEquals(expectedAns,goingToSendToServer);

	}
	@Test
	public void testBackToViewForum(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String [] initParseInput = "1. view_Forums\n".split(" ");
		parser.initParse(initParseInput);
		String demoServerMsg = "threadsList"+serverDelimiter+"admin"+serverDelimiter+"1:temp1"+serverDelimiter;
		TelnetClient.userName = "admin";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String goingToSendToServer = parser.parserUserMsg(2, TelnetClient.menu.toString().split("\n"));
		initParseInput = "1. back\n".split(" ");
		goingToSendToServer = parser.initParse(initParseInput);
		String expectedAns = "viewForum"+serverDelimiter;
		assertEquals(expectedAns,goingToSendToServer);
		
	}
	@Test
	public void testBackToViewThread(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String [] initParseInput = "1. view_Forums\n".split(" ");
		parser.initParse(initParseInput);
		String demoServerMsg = "threadsList"+serverDelimiter+"admin"+serverDelimiter+"1:temp1"+serverDelimiter+"";
		TelnetClient.userName = "admin";
		tMsgHandler.parseServerMsg(demoServerMsg);
		parser.parserUserMsg(2, TelnetClient.menu.toString().split("\n"));
		demoServerMsg = "postList"+serverDelimiter+"admin"+serverDelimiter+"1:subject1"+(char)2+"body"+serverDelimiter;
		tMsgHandler.parseServerMsg(demoServerMsg);
		System.out.println(TelnetClient.menu);
		parser.parserUserMsg(2, TelnetClient.menu.toString().split("\n"));
		initParseInput = "1. back\n".split(" ");
		String goingToSendToServer = parser.initParse(initParseInput);
		String expectedAns = "viewForum"+serverDelimiter;
		assertEquals(expectedAns,goingToSendToServer);
		
	}


}
